import request from '@/utils/request'

// 客群库接口
export interface CustomerGroup {
    id: number
    title: string
    people_num: number
    tag: string[]
    type: string | number  // 添加type字段，支持字符串或数字类型
    type_words?: string    // 分类文字描述
    created_at: string
    updated_at: string
    name?: string  // 保留兼容性
    tags?: string[] // 保留兼容性
    account_ids?: (string | number)[] // 账号ID数组
    costom?: Array<{ account_id: string | number }> // costom数组，包含account_id
}

// 联系人接口
export interface Contact {
    id: number
    library_id: number
    contact_info: string
    batchId: string
    status: 'success' | 'failed' | 'pending'
    importTime: string
    failReason?: string
}

// 导入批次接口
export interface ImportBatch {
    id: string
    customerGroupId: number
    totalCount: number
    successCount: number
    failCount: number
    status: 'importing' | 'completed' | 'failed'
    importTime: string
}

// 获取客群库列表
export const getCustomerGroupList = (params?: {
    page_no?: number
    page_size?: number
    title?: string
}) => {
    return request.get({
        url: '/audience.audienceLibrary/lists',
        params
    })
}

// 添加客群库
export const addCustomerGroup = (data: {
    title: string
    type: string | number
    tag: string[]
    account_ids?: string[]
}) => {
    return request.post({
        url: '/audience.audienceLibrary/add',
        data
    })
}

// 编辑客群库
export const updateCustomerGroup = (id: number, data: {
    id: number,
    title: string
    type: string | number
    tag: string[]
    account_ids?: string[]
}) => {
    return request.post({
        url: `/audience.audienceLibrary/edit`,
        data
    })
}

// 删除客群库
export const deleteCustomerGroup = (id: number) => {
    return request.post({
        url: `/audience.audienceLibrary/del`,
        data: { id }
    })
}

// 获取联系人列表
export const getContactList = (params: {
    library_id: number
    page_no?: number
    page_size?: number
    contact_info?: string
    status?: string
}) => {
    return request.get({
        url: '/audience.audienceContact/lists',
        params
    })
}

// 添加联系人
export const addContact = (data: {
    library_id: number
    contact_info_arr: string[] // 联系人信息数组
    tag?: string[] // 可选的标签数组
}) => {
    return request.post({
        url: '/audience.audienceContact/add',
        data
    })
}

// 编辑联系人
export const updateContact = (id: number, data: {
    contact_info: string
}) => {
    return request.post({
        url: `/audience.audienceContact/edit`,
        data: { id, ...data }
    })
}

// 删除联系人
export const deleteContact = (id: number) => {
    return request.post({
        url: `/audience.audienceContact/del`,
        data: { id }
    })
}

// Excel导入
export const importContactsFromExcel = (data: FormData) => {
    return request.post({
        url: '/audience.audienceContact/import',
        data,
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}

// 获取联系人状态统计
export const getContactStats = (library_id: number) => {
    return request.get({
        url: '/audience.audienceContact/charts',
        params: { library_id }
    })
}

// 获取客群库分享链接
export const getShareLink = (id: number) => {
    return request.post({
        url: '/audience.audienceLibrary/shareUrl',
        data: { id }
    })
}

// 获取广告组分享链接
export const getAdGroupShareLink = (id: number) => {
    return request.post({
        url: '/audience.audienceLibrary/adShareUrl',
        data: { id }
    })
}

// 获取客群库H5分享链接
export const getLibraryShareLink = () => {
    return request.post({
        url: '/audience.audienceLibrary/LibraryshareUrl'
    })
}


// 客户群-数量统计
export const getCount = () => {
    return request.get({
        url: '/audience.audienceLibrary/getCount'
    })
} 